package com.quotemanager.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.quotemanager.businessbean.ProductTypeBean;
import com.quotemanager.exception.DataAccessException;

public class ProductType implements ActionListener {

	JFrame productTypeFrame = null;
	JLabel productTypeLabel = null;
	JTextField productTypeField = null;
	JButton addButton = null;
	JButton cancelButton = null;
	JPanel dataPanel = null;
	JPanel buttonPanel = null;
	JPanel mainPanel =  null;
	
	/**
	 * @param args
	 */
	ProductType(){
		
		productTypeLabel =  new JLabel("Product Type ");
		productTypeField =  new JTextField(10);
		
		dataPanel = new JPanel(new GridLayout(1,2));
		dataPanel.add(productTypeLabel);
		dataPanel.add(productTypeField);
		
		addButton = new JButton("Add");
		cancelButton = new JButton("Cancel");
		addButton.addActionListener(this);
		cancelButton.addActionListener(this);
		buttonPanel = new JPanel(new GridLayout(1,2));
		buttonPanel.add(addButton);
		buttonPanel.add(cancelButton);
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
		mainPanel.add(dataPanel);
		mainPanel.add(buttonPanel);
		
		
		productTypeFrame = new JFrame("Product Type");
		productTypeFrame.getContentPane().add(mainPanel);
		productTypeFrame.setSize(400, 200);
		//productTypeFrame.pack();
		productTypeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		productTypeFrame.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		new ProductType();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == addButton){
			String productTypeName = productTypeField.getText();
			ProductTypeBean prodTypeRegistration = new ProductTypeBean();
			boolean insertStatus = false;
			/*try {
				insertStatus = prodTypeRegistration.registerProduct(productTypeName);
			} catch (DataAccessException e1) {
				e1.printStackTrace();
			}*/
			if(insertStatus)
				JOptionPane.showMessageDialog(productTypeFrame, "Product Type Registered Successfully");
			else
				JOptionPane.showMessageDialog(productTypeFrame, "Error in Product Type Registration");
					
		}
		if(e.getSource() == cancelButton){
			productTypeField.setText("");
		}
	}

}
